﻿Imports DevComponents.Editors.DateTimeAdv
Imports DevComponents.DotNetBar.Controls
Imports HTLibrary
Imports TNCBAL

Public Class ucSearchFiles
    Private _enumFile As enumFILE
    Private _objFileController As FileController

    Public Sub InitControl(ByVal isExport As enumFILE, ByVal objFileController As FileController)
        If Not isExport Then
            Dim loc = txtCompany.Location
            Dim index = txtCompany.TabIndex
            txtCompany.Location = txtForeignCompany.Location
            txtCompany.TabIndex = txtForeignCompany.TabIndex
            txtForeignCompany.Location = loc
            txtForeignCompany.TabIndex = index
        End If

        _enumFile = isExport
        _objFileController = objFileController

        ResetControls(Me.Controls)
    End Sub

    Public btnSearchEvent_Click As EventHandler
    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        If btnSearchEvent_Click IsNot Nothing Then
            btnSearchEvent_Click(sender, e)
        End If
    End Sub

    Public btnCancelEvent_Click As EventHandler
    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        'Reset
        ResetControls(Me.Controls)
        
        If btnCancelEvent_Click IsNot Nothing Then
            btnCancelEvent_Click(sender, e)
        End If
    End Sub

    Private Sub ResetControls(ByRef controlCollect As ControlCollection)
        CViewer.ResetControls(controlCollect)
        rbtnAll.Checked = True
        rbtnAll2.Checked = True
    End Sub

    ''' <summary>
    ''' Query File với các điều kiện lọc (nếu có)
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function SelectCustomAllRecord() As IQueryable
        Dim FCLValue As Integer = -1
        If rbtnFCL.Checked Then
            FCLValue = 1
        ElseIf rbtnLCL.Checked Then
            FCLValue = 0
        End If

        Dim D2DValue As Integer = -1
        If rbtnD2D.Checked Then
            D2DValue = 1
        ElseIf rbtnCY2CY.Checked Then
            D2DValue = 0
        End If

        Dim cbm_hbl, nw_hbl, gw_hbl As Double
        Dim cbm_cont, nw_cont, gw_cont As Double
        cbm_hbl = CViewer.GetNumberValue_TextBox(txtCBM_HBL)
        nw_hbl = CViewer.GetNumberValue_TextBox(txtNW_HBL)
        gw_hbl = CViewer.GetNumberValue_TextBox(txtGW_HBL)

        cbm_cont = CViewer.GetNumberValue_TextBox(txtCBM_Cont)
        nw_cont = CViewer.GetNumberValue_TextBox(txtNW_Cont)
        gw_cont = CViewer.GetNumberValue_TextBox(txtGW_Cont)

        Dim isMaster As Integer = -1
        If chkIsMaster.CheckState = CheckState.Checked Then
            isMaster = 1
        ElseIf chkIsMaster.CheckState = CheckState.Unchecked Then
            isMaster = 0
        End If

        Dim isCareOf As Integer = -1
        If chkIsCareOf.CheckState = CheckState.Checked Then
            isCareOf = 1
        ElseIf chkIsCareOf.CheckState = CheckState.Unchecked Then
            isCareOf = 0
        End If

        Dim query As IQueryable = _objFileController.SelectCustomAllRecord(_enumFile, _
                                                                           D2DValue, FCLValue, _
                                                                           txtFileNo.Text.Trim(), txtCustomer.Text, txtCompany.Text, txtForeignCompany.Text, txtAgent.Text, _
                                                                           cmbNoiHaCont.Text, cmbNoiNangCont.Text, _
                                                                           cmbTerm.Text, cmbCurrency.Text, _
                                                                           txtLineBookingNo.Text, txtMBLNo.Text, cmbLine.Text, cmbVessel.Text, txtVoyageNo.Text, _
                                                                           cmbPOL.Text, cmbPOD.Text, cmbTransshipmentPort.Text, _
                                                                           dtETDFrom.Value, dtETDTo.Value, dtETAFrom.Value, dtETATo.Value, _
                                                                           isMaster, isCareOf, _
                                                                            txtHBLNo.Text, _
                                                                            cmbConditionCBM_HBL.Text, cbm_hbl, _
                                                                            cmbConditionNW_HBL.Text, nw_hbl, _
                                                                            cmbConditionGW_HBL.Text, gw_hbl, _
                                                                            txtNotifyParty.Text, txtMarksNumbers.Text, txtGoodDescription.Text, _
                                                                            txtContNo.Text, cmbContType.Text, txtSeal.Text, cmbGood.Text, _
                                                                            cmbUnit.Text, cmbConditionCBM_Cont.Text, cbm_cont, cmbConditionNW_Cont.Text, nw_cont, cmbConditionGW_Cont.Text, gw_cont, txtContNote.Text)
        Return query
    End Function

    ''' <summary>
    ''' Khởi tạo dữ liệu mặc định của MBL Search
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub InitSearchData()
        TextBoxHelper.TextBox_InitUpperStringEvent(Me.Controls)

        'File
        CViewer.BindingComboBox_VietNamPort(cmbNoiHaCont)
        CViewer.BindingComboBox_VietNamPort(cmbNoiNangCont)
        CViewer.BindCurrencyToComboBox(cmbCurrency)

        'MBL
        InitSearch_MBLData()

        'HBL

        'Container
        CViewer.BindContTypeToComboBox(cmbContType)
        CViewer.BindGoodToComboBox(cmbGood)
        CViewer.BindUnitToComboBox(cmbUnit)
    End Sub

    ''' <summary>
    ''' Khởi tạo dữ liệu mặc định của MBL Search
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub InitSearch_MBLData()
        'Nếu là hàng xuất thì POL là các cảng Việt Nam, POD là các cảng nước ngoài
        If _enumFile = enumFILE.Export Then
            'POL
            CViewer.BindingComboBox_VietNamPort(cmbPOL)
            'POD
            CViewer.BindingComboBox_ForeignPort(cmbPOD)
        Else 'Nếu là hàng nhập thì POL là các cảng nước ngoài ,POD là các cảng Việt Nam
            'POL
            CViewer.BindingComboBox_ForeignPort(cmbPOL)
            'POD
            CViewer.BindingComboBox_VietNamPort(cmbPOD)
        End If

        'TransshipmentPort
        CViewer.BindingComboBox_ForeignPort(cmbTransshipmentPort)
        'Line
        CViewer.BindingComboBox_Line(cmbLine)
        'Vessel
        CViewer.BindingComboBox_Vessel(cmbVessel)
    End Sub

    Private Sub ucSearchFiles_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub chkSameAsConsignee_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkSameAsConsignee.CheckedChanged
        If chkSameAsConsignee.Checked Then
            txtNotifyParty.Text = "SAME AS CONSIGNEE"
        End If
    End Sub

    Private Sub txtNotifyParty_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtNotifyParty.Leave
        If txtNotifyParty.Text <> "SAME AS CONSIGNEE" Then
            chkSameAsConsignee.Checked = False
        End If
    End Sub

    Private Sub GroupBoxFCL_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBoxFCL.Enter

    End Sub
End Class
